package com.reader.core.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * md5加密.
 *
 * @author liujianqing
 * @date 2018/10/24
 */
public class Md5Util {

    public static String encrypt(String... strInput) {
        if(strInput==null || strInput.length==0){
            return "";
        }

        StringBuilder strSrc = new StringBuilder();
        for(String str : strInput){
            strSrc.append(str);
        }

        MessageDigest md;
        String strDes = null;
        byte[] bt = strSrc.toString().getBytes();
        try {
            md = MessageDigest.getInstance("MD5");
            md.update(bt);
            strDes = bytes2Hex(md.digest());
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
        return strDes;
    }

    // 将字节数组转换成16进制的字符串
    private static String bytes2Hex(byte[] bts) {
        String des = "";
        String tmp = null;
        for (int i = 0; i < bts.length; i++) {
            tmp = (Integer.toHexString(bts[i] & 0xFF));
            if (tmp.length() == 1) {
                des += "0";
            }
            des += tmp;
        }
        return des;
    }
}
